<?php
/**
* 登录相关函数
* $Id: function_login.php 154 2010-05-06 02:45:29Z bwanglei $
*/

/**
* 登录检查函数
*/
function apf_login($username, $password, $mysql_conn) {
	if (trim($username) == '' || trim($password) == '') {
		return false;
	} else {
		$user_data = apf_get_user($username, $mysql_conn);
		if (md5($password) == $user_data['base']['password']) {
			apf_set_login($user_data);
		} else {
			return false;
		}
	}
	return $user_data;
}

/**
* 根据用户名获取用户数据
* @para $username string
* @return user_data array
*/
function apf_get_user($username, $mysql_conn) {
	// 获取基本用户信息
	$query = "select * from `apf_user` where `username`='".$username."'";
	$result = @$mysql_conn->query($query);
	$user_data = array();
	if (isset($result) && ($result->num_rows > 0)) {
		$result = $result->fetch_assoc();
		$user_data['base'] = $result;
	}

	// 获取附加用户信息
	$query_meta = "select meta_key,meta_value from `apf_usermeta` where `username`='".$user_data['base']['username']."'";
	$result_meta = @$mysql_conn->query($query_meta);
	if (isset($result_meta)) {
		$array_user_meta = apf_array_table($result_meta, 'meta_key', 'meta_value');
		$user_data['meta'] = $array_user_meta;
	}

	// 获取版主信息
	$query_banzhu = "select banmian_id from `apf_banzhu_banmian` where `username`='".$user_data['base']['username']."'";
	$result_banzhu = @$mysql_conn->query($query_banzhu);
	if ($result_banzhu) {
		while ($result_banzhu_row = $result_banzhu->fetch_assoc()) {
			$user_data['banzhu'][] = $result_banzhu_row['banmian_id'];
		}
	}
	return $user_data;
}

/**
* 设置 cookie 用户信息
*/
function apf_set_login($user_data) {
	Global $apf_settings;
	foreach ($user_data['base'] as $user_data_name => $user_data_value) {
		$_SESSION['is_login'] = 'true';
		$_SESSION[$user_data_name] = $user_data_value;
		if ($user_data_name == 'username')
			setcookie($user_data_name, $user_data_value, time()+3600*24, $apf_settings['base_url']);
		if ($user_data_name == 'password')
			setcookie($user_data_name, md5($user_data_value.md5('wanglei')), time()+3600*24, $apf_settings['base_url']);
	}
	if (isset($user_data['meta'])) {
		foreach ($user_data['meta'] as $user_data_meta_key => $user_data_meta_value) {
			$_SESSION[$user_data_meta_key] = $user_data_meta_value;
			//setcookie($user_data_meta_key, $user_data_meta_value, time()+3600*24, $apf_settings['base_url']);
		}
	}
	if (isset($user_data['banzhu'])) {
		$_SESSION['banzhu'] = serialize($user_data['banzhu']);
		//setcookie('banzhu', serialize($user_data['banzhu']), time()+3600*24, $apf_settings['base_url']);
	}
}

/**
* 检查是否已经登录，并返回
*/
function apf_is_login() {
	global $mysql_conn;
	if (isset($_SESSION['is_login']) && $_SESSION['is_login']=='true') return true;
	if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
		$query = 'select `password` from `apf_user` where `username`="'.$_COOKIE['username'].'"';
		$result = $mysql_conn->query($query);
		if ($result->num_rows==1) {
			$result = $result->fetch_assoc();
			$result = $result['password'];
			if ($_COOKIE['password']==md5($result.md5('wanglei'))) {
				$user_data = apf_get_user($_COOKIE['username'], $mysql_conn);
				apf_set_login($user_data);
				return true;
			}
		}
	}
	return false;
}

/**
* 退出登录
*/
function apf_logout() {
	global $apf_settings;
	$_SESSION = array();
	session_unset();
	setcookie('username','logout',time()-3600,$apf_settings['base_url']);
	setcookie('password','logout',time()-3600,$apf_settings['base_url']);
}
?>
